//
//  Define.swift
//  
//
//  Created by S JZ on 2024/8/20.
//

import Foundation

#if os(iOS)
import UIKit
#endif

public class Define {
    public static var shared: Define = Define()
    
#if os(iOS)
    // 屏幕高度
    public var ScreenWidth: CGFloat { keyWidnow?.frame.size.width ?? 0 }
    // 屏幕宽度
    public var ScreenHeight: CGFloat { keyWidnow?.frame.size.height ?? 0 }
    
    // 状态栏高度
    public var StatusBarHeight: CGFloat { keyWidnow?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0 }
    // 导航栏高度
    // UIApplication.shared.statusBarFrame.height
    public var NavigationHeight: CGFloat {
        StatusBarHeight + UINavigationController().navigationBar.frame.size.height ?? 0
    }
    
    // 底部安全区域高度
    public var BottomSafeHeight: CGFloat { keyWidnow?.safeAreaInsets.bottom ?? 0 }
    // TabBar高度
    public var TabBarHeight: CGFloat { UITabBarController().tabBar.frame.size.height }
    
    public var Scare: CGFloat { windowScene?.screen.scale ?? 1 } // UISCreen.main.scale
    public var LineHeight: CGFloat { CGFloat(Scare >= 1 ? 1 / Scare : 1 ) }
    
    // MARK: - 屏幕宽高比例
    // 设置默认宽度为375.0
    public var setDefaultWidth: CGFloat {
        get { defaultWidth }
        set { defaultWidth = newValue }
    }
    var defaultWidth: CGFloat = 375.0
    // 当前屏幕 与 默认宽度 比利
    public var fit: CGFloat { ScreenWidth / defaultWidth }
#else
    public var Scare: CGFloat { 1 }
    public var fit: CGFloat { 1 }
#endif
}


#if os(iOS)

public extension Define {
    public var windowScene: UIWindowScene? {
        let windowSceneArr = UIApplication.shared.connectedScenes
            .filter({$0.activationState == .foregroundActive})
            .map({ $0 as? UIWindowScene })
            .compactMap({$0 })
        
        if !windowSceneArr.isEmpty {
            return windowSceneArr.first
        }
        return nil
    }
    
    // 获取Key window
    public var keyWidnow: UIWindow? {
        if let window = windowScene?.windows.filter({ $0.isKeyWindow }), !window.isEmpty {
            return window.first
        }else if let window = UIApplication.shared.delegate?.window {
            // iOS 13以下，也是这种获取方式
            return window
        }
        
        return nil
    }
}



public extension Define {
    public func currentController(base: UIViewController? = Define.shared.keyWidnow?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return currentController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            return currentController(base: tab.selectedViewController)
        }
        if let presented = base?.presentedViewController {
            return currentController(base: presented)
        }
        if let split = base as? UISplitViewController {
            return currentController(base: split.presentingViewController)
        }
        
        return base
    }
}

#endif



public extension Define {
    // 获取App信息
    public var infoDict: [String : Any]? { Bundle.main.infoDictionary }
       
    // 获取app的版本号
    public var appVersion: String { (infoDict?["CFBundleShortVersionString"] as? String) ?? "" }
       
    // 获取app的名称
    public var appName: String { (infoDict?["CFBundleDisplayName"] as? String) ?? "" }
       
    // 获取app的BundleId
    public var appBundleId: String { Bundle.main.bundleIdentifier ?? "" }
       
    #if os(iOS)
    // 获取设备名称，这个是用户自己定义的
    public var deviceName: String { UIDevice.current.name }
    // 系统名称
    public var systemName: String { UIDevice.current.systemName }
    // 系统版本
    public var systemVersion: String { UIDevice.current.systemVersion }
    #endif
}

